package com.soboot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.soboot.base.converter.BeanMapping;
import com.soboot.base.model.BaseController;
import com.soboot.base.model.BasePageRes;
import com.soboot.response.Result;
import com.soboot.system.entity.SysDict;
import com.soboot.system.entity.SysRole;
import com.soboot.system.model.DictSaveReqModel;
import com.soboot.system.model.QueryRoleListReqModel;
import com.soboot.system.model.RoleSaveReqModel;
import com.soboot.system.service.ISysRoleService;
import com.soboot.system.vo.RoleVO;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:
 * @createTime: 2022年12月06日 14:35:48
 * @version:
 * @Description:
 * @Copyright:
 */
@RestController
@RequestMapping("/roles")
@Api(value="角色管理接口", tags = {"角色管理"})
public class RoleController extends BaseController {

    @Autowired
    private ISysRoleService roleService;

    @ApiOperation(value = "分页查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reqModel", value = "查询实体", required = true, dataTypeClass = QueryRoleListReqModel.class)
    })
    @GetMapping("/page")
    public Result<BasePageRes<RoleVO>> page(QueryRoleListReqModel reqModel) {

        IPage<SysRole> page = roleService.page(reqModel);
        List<RoleVO> list = BeanMapping.mapList(page.getRecords(),RoleVO.class);
        BasePageRes<RoleVO> result = new BasePageRes<>(page.getCurrent(), page.getSize(), page.getTotal(), list);
        return Result.success(result);
    }

    @ApiOperation(value = "详细信息", notes = "详细信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<RoleVO> detail(@PathVariable Integer id) {

        RoleVO result = roleService.detail(id);
        return Result.success(result);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @ApiImplicitParam(name = "role", value = "role", required = true, dataType = "roleDto")
    @PostMapping()
    public Result<String> addRole(@RequestBody @ApiParam @Validated({Insert.class}) RoleSaveReqModel reqModel) {

        SysRole sysRole = BeanMapping.map(reqModel,SysRole.class);
        roleService.saveOrUpdate(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "更新", notes = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "role", value = "role", required = true, dataType = "roleDto")
    })
    @PutMapping("/{id}")
    public Result<String> updateRole(@PathVariable Integer id, @RequestBody @ApiParam @Validated({Update.class}) RoleSaveReqModel reqModel) {

        SysRole sysRole = BeanMapping.map(reqModel,SysRole.class);
        roleService.saveOrUpdate(sysRole);
        return Result.success();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Integer id) {

        roleService.delete(id);
        return Result.success();
    }
}
